package com.threegroup.common.utils;

import org.apache.commons.lang3.RandomStringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class OrderNumUtils {
    /**
     * 生成订单号（25位）：时间（精确到毫秒）+3位随机数+5位用户id
     */
    //public static synchronized  String getOrderNum(Long userId) {
    public static synchronized  String getOrderNum() {
        //时间（精确到毫秒）
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String localDate = LocalDateTime.now().format(ofPattern);
        //3位随机数
        String randomNumeric = RandomStringUtils.randomNumeric(6);
        //5位用户id
    //    int subStrLength = 5;
    //    String sUserId = userId.toString();
    //    int length = sUserId.length();
    //    String str;
    //    if (length >= subStrLength) {
    //        str = sUserId.substring(length - subStrLength, length);
    //    } else {
    //        str = String.format("%0" + subStrLength + "d", userId);
    //    }
    //    String orderNum = localDate + randomNumeric + str;
        String orderNum = localDate + randomNumeric;

        return orderNum;
    }
}
